﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using RobotGerchik.DataClass;
using RobotGerchik.DataClass.Gerchik;

namespace RobotGerchik.Views
{
    /// <summary>
    /// Логика взаимодействия для addNewGerchikRobot.xaml
    /// </summary>
    public partial class addNewGerchikRobot : Window
    {
        Data data;
        GechikRobotItem itemGerichikRobot;

        public addNewGerchikRobot(GechikRobotItem item, Data data, bool isedit)
        {
            InitializeComponent();

            this.data = data;
            itemGerichikRobot = item;



            //var dfd = data.GetClientCode();
            cmbxClientCode.ItemsSource = data.GetClientCode();
            cmbxClientCode.SelectedIndex = 0;
            if (String.IsNullOrEmpty(item.ClientCode))
                cmbxClientCode.SelectedValue = item.ClientCode;

            cmbxMarket.ItemsSource = data.GetMarkets();
            cmbxMarket.SelectedIndex = 0;
            if (item.Market != null)
                cmbxMarket.SelectedValue = item.Market;

            if (!String.IsNullOrEmpty(cmbxMarket.Text))
            {
                cmbxInstrument.ItemsSource = data.GetInstrumets(cmbxMarket.Text);
                cmbxInstrument.SelectedIndex = 0;
            }
            if (item.Instrument != null)
                cmbxInstrument.SelectedValue = item.Instrument;

            cmbxTimeFrame.ItemsSource = data.GetTimeFrame();
            cmbxTimeFrame.SelectedIndex = 0;
            if (item.TimeFrame != null)
                cmbxTimeFrame.SelectedValue = item.TimeFrame;

            txbxThirdBarDeviation.Text = item.ThirdBarDeviation.ToString();
            txbxKillOrderPoint.Text = item.KillOrderPoint.ToString();
            txbxStopLimiOtstup.Text = item.StopLimiOtstup.ToString();
            txbxBreakeven.Text = item.Breakeven.ToString();
            txbxProfit.Text = item.Profit.ToString();
            txbxMaxExcess.Text = item.MaxExcess.ToString();

            txbxTimeStartLevel.Text = item.TimeStartLevel.TimeOfDay.ToString();
            txbxTimeStopLevel.Text = item.TimeStopLevel.TimeOfDay.ToString();
            txbxTimeClosePoz.Text = item.TimeClosePoz.TimeOfDay.ToString();
            txbxQuantity.Text = item.Quantity.ToString();
            txbxAttemptAmount.Text = item.AttemptAmount.ToString();

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                itemGerichikRobot.ClientCode = cmbxClientCode.Text;
                itemGerichikRobot.Market = cmbxMarket.Text;
                itemGerichikRobot.Instrument = cmbxInstrument.Text;
                itemGerichikRobot.TimeFrame = cmbxTimeFrame.Text;

                itemGerichikRobot.ThirdBarDeviation = Convert.ToDouble(txbxThirdBarDeviation.Text);
                itemGerichikRobot.KillOrderPoint = Convert.ToDouble(txbxKillOrderPoint.Text);
                itemGerichikRobot.StopLimiOtstup = Convert.ToDouble(txbxStopLimiOtstup.Text);
                itemGerichikRobot.Profit = Convert.ToDouble(txbxProfit.Text);
                itemGerichikRobot.MaxExcess = Convert.ToDouble(txbxMaxExcess.Text);
                itemGerichikRobot.Breakeven = Convert.ToDouble(txbxBreakeven.Text);
                itemGerichikRobot.Quantity = Convert.ToInt32(txbxQuantity.Text);

                itemGerichikRobot.TimeStartLevel = Convert.ToDateTime(txbxTimeStartLevel.Text);
                itemGerichikRobot.TimeStopLevel = Convert.ToDateTime(txbxTimeStopLevel.Text);
                itemGerichikRobot.TimeClosePoz = Convert.ToDateTime(txbxTimeClosePoz.Text);
                itemGerichikRobot.AttemptAmount = Convert.ToInt32(txbxAttemptAmount.Text);

                Close();
            }
            catch
            { }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }



        private void cmbxClientCode_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void cmbxMarket_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (cmbxMarket.Items.Count > 0)
                    if (!String.IsNullOrEmpty(cmbxMarket.SelectedItem.ToString()))
                    {

                        cmbxInstrument.ItemsSource = data.GetInstrumets(cmbxMarket.SelectedItem.ToString());
                        cmbxInstrument.SelectedIndex = 0;
                    }
            }
            catch
            { }
        }

        private void cmbxInstrument_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }


        private void txbx_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!Char.IsDigit(e.Text, 0))
                e.Handled = true;

            if ((e.Text.IndexOf('.') == 0 || e.Text.IndexOf(',') == 0) && ((TextBox)sender).Text.IndexOf('.') == -1 && ((TextBox)sender).Text.IndexOf(',') == -1)
                e.Handled = false;


        }

        private void txbx_GotFocus(object sender, RoutedEventArgs e)
        {

            try
            {


                if (((TextBox) sender).Text != "")
                {
                    if (Convert.ToDouble(((TextBox) sender).Text) == 0)
                    {
                        ((TextBox) sender).Text = "";
                    }

                }
            }
            catch
            {
                ((TextBox) sender).Text = "";
            }
        }

        private void txbxDitgit_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!Char.IsDigit(e.Text, 0))
                e.Handled = true;
        }

        private void txbx_LostFocus(object sender, RoutedEventArgs e)
        {
            if (((TextBox)sender).Text == "")
                ((TextBox)sender).Text = 0.ToString();

            char PointChar = ',';
            string str = (1.2).ToString();
            if (str.IndexOf('.') > 0) PointChar = '.';

            if (PointChar == ',')
                ((TextBox)sender).Text=((TextBox)sender).Text.Replace('.', PointChar);

            if (PointChar == '.')
                ((TextBox)sender).Text = ((TextBox)sender).Text.Replace(',', PointChar);
        }


    }
}
